import {request, jsonRequest} from '../../utils/request'
import url from '../constants/url'
import MessageActions from '../actions/message'

export default function sendCode(data){
    return new Promise((resolve, reject) => {
        if(!/^0?(13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])[0-9]{8}$/.test(data.phone)) return reject('手机号码错误');
        if(!data.imageNo) return reject('请填写验证码');
        jsonRequest
        .post(url.verifications_sendcode, data)
        .then(function(res){
            const body = res.data;
            const message = body.responseCode !== '00' ? body.responseMessage : null;
            if(message) {
                MessageActions.show({'message': message});
            }else {
                return resolve();
            }
        })
        .catch(reject);
    })
}
